{include file="public/toper"/}
<style>
    .layui-unselect{width: 190px;}
    .layui-layedit-tool {width: 99%;}
</style>
<div class="x-body">
    <form action="" method="post" class="layui-form" id="forms">
        <div class="layui-form-item">
            <label for="name" class="layui-form-label">
                <span class="x-red">*</span>名称
            </label>
            <div class="layui-input-inline">
                <input type="text" value="" id="name" name="name" required="" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>10个字符以内
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>图片
            </label>
            <div class="layui-input-inline layui-upload" id="upload-thumb">
                <button type="button" class="layui-btn layui-btn-primary" id="image"><i class="layui-icon"></i>上传图片</button>
                <div class="layui-upload-list">
                    <img class="layui-upload-list imgsc" id="demo" width="100" height="100">
                    <p id="demoText"></p>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>列表小图
            </label>
            <div class="layui-input-inline layui-upload" id="upload-thumbs">
                <button type="button" class="layui-btn layui-btn-primary" id="images"><i class="layui-icon"></i>上传图片</button>
                <div class="layui-upload-list">
                    <img class="layui-upload-list imgsc" id="demos" width="100" height="100">
                    <p id="demoTexts"></p>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>类型</label>
            <div class="layui-input-block">
                <select name="type">
                    <option value="0">{:MCARD_TYPE0}</option>
                    <option value="1">{:MCARD_TYPE1}</option>
                    <option value="2">{:MCARD_TYPE2}</option>
                    <option value="3">{:MCARD_TYPE3}</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>有效期</label>
            <div class="layui-input-block">
                <select name="expiry">
                    <option value="0">{:MCARD_EXPIRY0}</option>
                    <option value="1">{:MCARD_EXPIRY1}</option>
                    <option value="2">{:MCARD_EXPIRY2}</option>
                    <option value="3">{:MCARD_EXPIRY3}</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="price" class="layui-form-label">
                <span class="x-red">*</span>销售价
            </label>
            <div class="layui-input-inline">
                <input type="number" value="" min="1" id="price" name="price" required="" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="benefit_price" class="layui-form-label">
                <span class="x-red"></span>优惠价
            </label>
            <div class="layui-input-inline">
                <input type="number" value="" id="benefit_price" name="benefit_price" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="give" class="layui-form-label">
                <span class="x-red"></span>赠送时长
            </label>
            <div class="layui-input-inline">
                <input type="number" value="" min="1" id="give" name="give" required="" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*天</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="one_commission" class="layui-form-label">
                <span class="x-red">*</span>一级佣金
            </label>
            <div class="layui-input-inline">
                <input type="number" value="" min="1" id="one_commission" name="one_commission" required="" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="two_commission" class="layui-form-label">
                <span class="x-red">*</span>二级佣金
            </label>
            <div class="layui-input-inline">
                <input type="number" value="" min="1" id="two_commission" name="two_commission" required="" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="exempted_quota" class="layui-form-label">
                <span class="x-red">*</span>免押额度
            </label>
            <div class="layui-input-inline">
                <input type="number" value="" min="1" id="exempted_quota" name="exempted_quota" required="" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="name" class="layui-form-label">
                <span class="x-red"></span>描述
            </label>
            <div class="layui-input-inline" style="width: 50%">
                <textarea name="des" id="des" maxlength="255" style="height: 320px;width: 50%;"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>状态
            </label>
            <div class="layui-input-block">
                <input type="radio" name="state" value="0" checked="checked" title="{:MCARD_STATE0}">
                <input type="radio" name="state" value="1" title="{:MCARD_STATE1}">
                <input type="radio" name="state" value="2" title="{:MCARD_STATE2}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <button  class="layui-btn" lay-filter="formSubmit2" lay-submit="">提交</button>
        </div>
    </form>
</div>
<script type="text/javascript" src="/static/js/base64.js"></script>
<script>
    layui.use(['form','layer','upload','layedit'], function(){
        $ = layui.jquery;
        var form = layui.form
            ,layer = layui.layer,
            upload = layui.upload;
        var layedit = layui.layedit;


        layedit.set({
            uploadImage: {
                url:"{:url('Upload/uploadFiles')}"//接口url
                ,type: 'post' //默认post
            }
        });
        var des =  layedit.build('des'); //建立编辑器

        form.on('submit(formSubmit2)', function(data) {
            var _form = '';
            if ($(this).attr('data-form')) {
                _form = $($(this).attr('data-form'));
            } else {
                _form = $(this).parents('form');
            }
            layedit.sync(des);
            $('#des').val(base64.encode($('#des').val()));
            layer.msg('数据提交中...',{time:500000});
            $.ajax({
                type: "POST",
                url: _form.attr('action'),
                data: _form.serialize(),
                success: function(res) {
                    if(res.code == 200) {
                        layer.alert(res.msg, {icon: 6},function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                            parent.location.reload();
                        })
                    } else {
                        layer.msg(res.msg,{icon: 2});
                    }
                }
            });
            return false;
        });





        //普通图片上传
        var uploadInst = upload.render({
            elem: '#image',
            url: "{:url('Upload/uploadCard')}",
            ext: 'jpg|png|gif',
            drag:false,
            before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo').attr('src', result); //图片链接（base64）
                });
            },
            done: function(res){
                //如果上传失败
                if(res.code == 200 ){
                    $('#upload-thumb').append('<input type="hidden" name="img" value="'+ res.data.src+'">');
                }else {
                    layer.msg(res.msg);
                }
                //上传成功
            },
            error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });


        //普通图片上传
        var uploadInsts = upload.render({
            elem: '#images',
            url: "{:url('Upload/uploadCard')}",
            ext: 'jpg|png|gif',
            drag:false,
            before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demos').attr('src', result); //图片链接（base64）
                });
            },
            done: function(res){
                //如果上传失败
                if(res.code == 200 ){
                    $('#upload-thumbs').append('<input type="hidden" name="imgs" value="'+ res.data.src+'">');
                }else {
                    layer.msg(res.msg);
                }
                //上传成功
            },
            error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoTexts');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
    });

</script>

{include file="public/footer"/}